package cn.itcast.wanxinp2p.account.service.impl;

import cn.itcast.wanxinp2p.account.exception.AccountErrorCode;
import cn.itcast.wanxinp2p.common.domain.RestResponse;
import cn.itcast.wanxinp2p.common.exception.BusinessException;
import cn.itcast.wanxinp2p.common.util.OkHttpUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * TODO
 *
 * @Description
 * @Author Administrator
 * @Date 2023/8/28 12:55
 **/
@Service
public class SmsServiceImpl {
    @Value("${sms.url}")
    private String URL;
    @Value("${sms.enable}")
    private Boolean enable;

    //获取短信验证码
    public RestResponse getSMSCode(String mobile) {
        if (enable) {
         return   OkHttpUtil.post(URL + "generate?effectiveTime=300&name=sms", "{\"mobile\":" + mobile + "}");
        }
        return RestResponse.success();
    }
    //校验验证码
    public void checkCode(String key, String code) {
        //验证码服务发送请求
        if (enable) {
            RestResponse post = OkHttpUtil.post(URL + "verify?name=sms & verificationKey=" + key + "&verificationCode=" + code, "");
            if (post.getCode()!=0||post.getResult().toString().equalsIgnoreCase("false")){
                 new BusinessException(AccountErrorCode.E_140152);
            }
        }

    }
}
